package games.gong.durid.controller;

import games.gong.durid.common.R;
import games.gong.durid.common.PermissionConstants;
import games.gong.durid.entity.RolePermission;
import games.gong.durid.service.RolePermissionService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Tag(name = "角色权限管理", description = "角色权限关联管理接口")
@RestController
@RequestMapping("/api/role-permissions")
public class RolePermissionController {
    
    @Autowired
    private RolePermissionService rolePermissionService;
    
    @Operation(summary = "创建角色权限关联")
    @PostMapping
    @PreAuthorize("@ss.hasPermission('" + PermissionConstants.ROLE_PERMISSION_CREATE + "')")
    public R<RolePermission> createRolePermission(
            @Parameter(description = "角色权限关联信息") @RequestBody RolePermission rolePermission) {
        int result = rolePermissionService.addRolePermission(rolePermission);
        if (result > 0) {
            return R.success("角色权限关联创建成功", rolePermission);
        } else {
            return R.error("角色权限关联创建失败");
        }
    }
    
    @Operation(summary = "删除角色权限关联")
    @DeleteMapping("/{id}")
    @PreAuthorize("@ss.hasPermission('" + PermissionConstants.ROLE_PERMISSION_DELETE + "')")
    public R<String> deleteRolePermission(
            @Parameter(description = "角色权限关联ID") @PathVariable Long id) {
        int result = rolePermissionService.deleteRolePermission(id);
        if (result > 0) {
            return R.success("角色权限关联删除成功");
        } else {
            return R.error("角色权限关联删除失败");
        }
    }
    
    @Operation(summary = "根据角色ID和权限ID删除角色权限关联")
    @DeleteMapping("/role/{roleId}/permission/{permissionId}")
    @PreAuthorize("@ss.hasPermission('" + PermissionConstants.ROLE_PERMISSION_DELETE + "')")
    public R<String> deleteRolePermissionByRoleIdAndPermissionId(
            @Parameter(description = "角色ID") @PathVariable Long roleId,
            @Parameter(description = "权限ID") @PathVariable Long permissionId) {
        int result = rolePermissionService.deleteRolePermissionByRoleIdAndPermissionId(roleId, permissionId);
        if (result > 0) {
            return R.success("角色权限关联删除成功");
        } else {
            return R.error("角色权限关联删除失败");
        }
    }
    
    @Operation(summary = "获取角色权限关联详情")
    @GetMapping("/{id}")
    @PreAuthorize("@ss.hasPermission('" + PermissionConstants.ROLE_PERMISSION_READ + "')")
    public R<RolePermission> getRolePermission(
            @Parameter(description = "角色权限关联ID") @PathVariable Long id) {
        RolePermission rolePermission = rolePermissionService.getRolePermissionById(id);
        if (rolePermission != null) {
            return R.success(rolePermission);
        } else {
            return R.error("角色权限关联不存在");
        }
    }
    
    @Operation(summary = "根据角色ID获取角色权限关联列表")
    @GetMapping("/role/{roleId}")
    @PreAuthorize("@ss.hasPermission('" + PermissionConstants.ROLE_PERMISSION_READ + "')")
    public R<List<RolePermission>> getRolePermissionsByRoleId(
            @Parameter(description = "角色ID") @PathVariable Long roleId) {
        List<RolePermission> rolePermissions = rolePermissionService.getRolePermissionsByRoleId(roleId);
        return R.success(rolePermissions);
    }
    
    @Operation(summary = "根据权限ID获取角色权限关联列表")
    @GetMapping("/permission/{permissionId}")
    @PreAuthorize("@ss.hasPermission('" + PermissionConstants.ROLE_PERMISSION_READ + "')")
    public R<List<RolePermission>> getRolePermissionsByPermissionId(
            @Parameter(description = "权限ID") @PathVariable Long permissionId) {
        List<RolePermission> rolePermissions = rolePermissionService.getRolePermissionsByPermissionId(permissionId);
        return R.success(rolePermissions);
    }
}